﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public class ControlHelper
    {
        public static Control[] GetAllControl(Control.ControlCollection Collection)
        {
            var list = new List<Control>();
            return GetAllControl(Collection, null, list).ToArray();
        }

        public static Control[] GetAllControl(Control.ControlCollection Collection, Type Type)
        {
            var list = new List<Control>();
            return GetAllControl(Collection, Type, list).ToArray();
        }

        private static List<Control> GetAllControl(Control.ControlCollection Collection, Type Type, List<Control> List)
        {
            foreach (Control ct in Collection)
            {
                if (ReferenceEquals(ct.GetType(), Type))
                {
                    List.Add((Control)ct);
                }
                else if (ct.HasChildren)
                {
                    GetAllControl((Control.ControlCollection)ct.Controls, Type, List);
                }
                else if (Type is null)
                {
                    List.Add((Control)ct);
                }
            }

            return List;
        }
    }
}